package com.socurites.example.esper.engine;

import com.espertech.esper.client.Configuration;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;
import com.socurites.example.esper.event.OrderEvent;
import com.socurites.example.esper.listener.MyListener;

public class MyEsperEngine {
    public static void main(String[] args) {
        // <Setting a Configuration>
        Configuration config = new Configuration();
        config.addEventTypeAutoName("com.socurites.example.esper.event");
        // </Setting a Configuration>

        // <Creating a Statement>
        EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider(config);
        String epl = "select avg(price) from OrderEvent.win:time(30 sec)";
        EPStatement statement = epService.getEPAdministrator().createEPL(epl);
        // </Creating a Statement>
        
        // <Adding a Listener>
        MyListener listener = new MyListener();
        statement.addListener(listener);
        // </Adding a Listener>

        // <Sending events>
        OrderEvent event1 = new OrderEvent("shirt", 74.50);
        epService.getEPRuntime().sendEvent(event1);

        OrderEvent event2 = new OrderEvent("pants", 26.50);
        epService.getEPRuntime().sendEvent(event2);
        // </Sending events>
    }
}
